package Bank;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Scanner; //用户类。





/**
 * 用户类
 * 用户定义了用户存取款，查看余额，查看交易记录，修改密码的功能
 * @date	2014.08.17
 */
public class Customer {

	static long CUSTOMER_ID;
	private long customer_id;
	private String customer_name;
	private String customer_lastname;
	private String customer_username;
	private String customer_password;
	private String customer_password1="\\d{6}";   //用于判断标准6位数字密码。
	private String customer_phonenumber;
	private String customer_address;
	private int count=1;
	static private int cuCount = 0;
	//Account account[] = MainTest.AccountArray;
	Scanner scan=new Scanner(System.in);
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
	/**
	 * 测试用构造方法
	 */
	public Customer(long id,String customer_name,String customer_lastname,String customer_username, String customer_password,String customer_phonenumber,String customer_address){
		this.setCustomer_id(id);
		this.customer_name = customer_name;
		this.customer_lastname = customer_lastname;
		this.customer_username = customer_username;
		this.customer_password = customer_password;
		this.customer_phonenumber = customer_phonenumber;
		this.customer_address = customer_address;
	}
	public Customer(){
	}
		
	public long getCustomer_id() {
		return this.customer_id;
	}
	public void setCustomer_id(long customer_id) {
		this.customer_id = customer_id;
		cuCount++;	//每次初始化一个，数量加1
	}
	static public int getCount(){
		return cuCount;
	}
	public String getCustomer_name() {
		return customer_name;
	}
	public void setCustomer_name(String customer_name) {
		this.customer_name = customer_name;
	}
	public String getCustomer_lastname() {
		return customer_lastname;
	}
	public void setCustomer_lastname(String customer_lastname) {
		this.customer_lastname = customer_lastname;
	}
	public String getCustomer_username() {
		return customer_username;
	}
	public void setCustomer_username(String customer_username) {
		this.customer_username = customer_username;
	}
	public String getCustomer_password() {
		return customer_password;
	}
	public void setCustomer_password(String customer_password) {
		this.customer_password = customer_password;
	}
	public String getCustomer_address() {
		return customer_address;
	}
	public String getCustomer_phonenumber() {
		return customer_phonenumber;
	}
	public void setCustomer_phonenumber(String customer_phonenumber) {
		this.customer_phonenumber = customer_phonenumber;
	}
	public void setCustomer_address(String customer_address) {
		this.customer_address = customer_address;
	}
		
	public void changePassword(){
		System.out.println("Enter your password:");		
		String str1 = scan.next();		
		String str2 =null, str3 =null;
		
		//int CUSTOMER_ID=111; 		//测试专用
		//JBDC.open();
		//找出数据库的密码
		String Query="Select CUSTOMER_PASSWORD from CUSTOMER_TABLE where CUSTOMER_ID="+CUSTOMER_ID;			
		
		//ResultSet rs = null;
		String s;
			
		try {
			ResultSet rs = new ConnDB().getrs(Query);
			//rs = JBDC.stmt.executeQuery(Query);
			while(rs.next()){
				s=rs.getString("CUSTOMER_PASSWORD");
				//让数据库中的密码与输入的密码比较
				//两者一至，允许修改密码
				if( str1.equals(s) ){
					System.out.println("Password is correct!");
					System.out.println("Enter your new password:");
					str2=scan.next();
					//输入的新密码必须是六位数字（customer_password1使用了正则表达式）
					if(str2.matches(customer_password1))
				       {  }
					else{
				    	   System.out.println("Error!! Please enter six numbers!!");
				    	   changePassword();
				       }
					System.out.println("Enter your new password again:");
					str3=scan.next();
					//新密码设置成功，在数据库中进行更新
					if( str2.equals(str3)){
						String update="update CUSTOMER_TABLE set CUSTOMER_PASSWORD='"+str2+"' where CUSTOMER_ID="+CUSTOMER_ID;
						//JBDC.stmt.executeUpdate(update);
						new ConnDB().uprs(update);
						
					}else{
						System.out.println("Your new password is not OK.  Exit.");
						changePassword();
					}
				}
				//输错4次，程序终止
				else{
					 count++;
					 if(count<4){
						 System.out.print("error. ");
					     changePassword();
				     }else{
				    	 System.out.println("You are wrong more than 3 times. Exit.");
				     }
				}			
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		//JBDC.close();
		
	}
		
//	/**
//	 * 查看账户信息
//	 * @param accountId
//	 */
//	public void checkBalance(long accountId) {
//		for(int i=0;i<Account.getCount();i++){
//			if(accountId == account[i].getAccount_id()){
//				account[i].checkBalance();
//			}
//		}		
//	}
//	
//	/**
//	 * 浏览交易记录，只有5条
//	 * @param accountId
//	 */
//	public void transactionHistory(long accountId) {
//		for(int i=0;i<Account.getCount();i++){
//			if(accountId == account[i].getAccount_id()){
//				account[i].viewTransactions();
//			}
//		}			
//	}
	
	/**
	 * 显示用户账户信息
	 */
	public void show() {
		//打开数据库
    	//JBDC.open();
    	try {
    		//查询账户并打印
    		String Query="Select * from TRANSACTION_TABLE where account_id="+Account.ACCOUNT_ID;			
    		ResultSet rs =new ConnDB().getrs(Query);
    		
    		while(rs.next()){
    			System.out.println("TRANSACTION_ID: "+rs.getInt("TRANSACTION_ID")
    					+"\tTRANSACTION_DATE: "+rs.getDate("TRANSACTION_DATE")
    					+"\tTRANSACTION_AMOUNT: "+rs.getFloat("TRANSACTION_AMOUNT")
    					+"\tTRANSACTION_TYPE: "+rs.getString("TRANSACTION_TYPE"));
    		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//关闭数据库
    	//JBDC.close();
		
	}
}
